// application.cpp
// Customizer_Pro
// Protea Customizer Pro / Business Logic for managing applications
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "pro_customizer.h"


//@ SEC 3
//@ CHA Appl Defn Add
//@ COD ApplDefnAdd
//@ TAB Appl_defn write
//@ GEN Use to add appl_defn records

void ApplDefnAdd( XCBuffer* appl_defn, XCTransactionVars* vars ) {

	XCDBStatement* stmt = NULL;
	XCBuffer* temp = NULL;
	XCSQLGen* query = vars->query;

	long last_number = 0;

	TRANS_INFUNC( ApplDefnAdd );

	//@ Determine the next available application number
	temp = App->CreateBuffer( FMT_APPL_DEFN );
	try {
		XCOrder ord;
		bool found = false;

		query->MakeSELECT( temp );
		query->AddORDERBY( FLD_APPL_NBR, &ord );
		stmt = vars->dbtxn->ExecuteSQL( query );
		while (!found && !stmt->Eof()) {
			long value = temp->GetField( FLD_APPL_NBR )->GetLong( true );
			if (value != last_number + 1) {
				// We found a blank space
				found = true;
			} else {
				last_number = value;
				stmt->MoveNext();
			};
		};

		// Cleanup now
		App->CleanupObjects( 2, &temp, &stmt );


		appl_defn->GetField( FLD_APPL_NBR )->SetLong( last_number + 1 );


		App->ToolsTrace( TRC_APPINTERFACE, TLV_HIGH, "******    APPLICATION #%ld", appl_defn->GetField( FLD_APPL_NBR )->GetLong( true ) );
	} catch( ... ) {
		App->CleanupObjects( 2, &temp, &stmt );
		throw;
	};
	


	//@ Add the application record
	query->MakeFull( SQL_STMT_INSERT, appl_defn );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};



//@ SEC 2
//@ CHA AddApplication
//@ COD AddApplication
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB APPL_DEFN Write
//
//@ GEN AddApplication will add an application record
//
TRANSACTION_CODE( AddApplication ) {
	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( AddApplication );
		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_APPL_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB ApplDefnVerify to verify that application can be added
			ApplDefnVerify( TransBuf, VERIFY_ADD, &txnvars );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB ApplDefnAdd to add the new application
			ApplDefnAdd( TransBuf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};
